﻿Public Class frmGridColor
    Private Const SAMPLE_ROW_COUNT As Integer = 30
    Private _ModalResult As DialogResult
    Private ReadOnly Property Settings() As TGridSettings
        Get
            Return frmMain.GridSettings
        End Get
    End Property
    Public Function ShowEx() As DialogResult
        _ModalResult = Windows.Forms.DialogResult.None
        InitFrm()
        Me.ShowDialog()
        Return _ModalResult
    End Function
    Private Sub InitFrm()
        Grid.SuspendLayout()
        Try
            RemoveEvents()
            InitCbo()
            InitSample()
            ApplySettings()
            Settings.Apply(Grid)
            AddEvents()
        Finally
            Grid.ResumeLayout()
        End Try
    End Sub

    Private Sub RemoveEvents()
        RemoveHandler cboHeaderFont.SelectedIndexChanged, AddressOf cboHeaderFont_SelectedIndexChanged
        RemoveHandler txtHeaderFontSize.ValueChanged, AddressOf txtHeaderFontSize_ValueChanged
        RemoveHandler cboEvenFont.SelectedIndexChanged, AddressOf cboEvenFont_SelectedIndexChanged
        RemoveHandler txtEvenFontSize.ValueChanged, AddressOf txtEvenFontSize_ValueChanged
        RemoveHandler cboOddFont.SelectedIndexChanged, AddressOf cboOddFont_SelectedIndexChanged
        RemoveHandler txtOddFontSize.ValueChanged, AddressOf txtOddFontSize_ValueChanged
        RemoveHandler chkHeaderBold.CheckedChanged, AddressOf chkHeaderBold_CheckedChanged
        RemoveHandler chkHeaderItalic.CheckedChanged, AddressOf chkHeaderItalic_CheckedChanged
        RemoveHandler chkEvenBold.CheckedChanged, AddressOf chkEvenBold_CheckedChanged
        RemoveHandler chkEvenItalic.CheckedChanged, AddressOf chkEvenItalic_CheckedChanged
        RemoveHandler chkOddBold.CheckedChanged, AddressOf chkOddBold_CheckedChanged
        RemoveHandler chkOddItalic.CheckedChanged, AddressOf chkOddItalic_CheckedChanged

    End Sub
    Private Sub AddEvents()
        AddHandler cboHeaderFont.SelectedIndexChanged, AddressOf cboHeaderFont_SelectedIndexChanged
        AddHandler txtHeaderFontSize.ValueChanged, AddressOf txtHeaderFontSize_ValueChanged
        AddHandler cboEvenFont.SelectedIndexChanged, AddressOf cboEvenFont_SelectedIndexChanged
        AddHandler txtEvenFontSize.ValueChanged, AddressOf txtEvenFontSize_ValueChanged
        AddHandler cboOddFont.SelectedIndexChanged, AddressOf cboOddFont_SelectedIndexChanged
        AddHandler txtOddFontSize.ValueChanged, AddressOf txtOddFontSize_ValueChanged
        AddHandler chkHeaderBold.CheckedChanged, AddressOf chkHeaderBold_CheckedChanged
        AddHandler chkHeaderItalic.CheckedChanged, AddressOf chkHeaderItalic_CheckedChanged
        AddHandler chkEvenBold.CheckedChanged, AddressOf chkEvenBold_CheckedChanged
        AddHandler chkEvenItalic.CheckedChanged, AddressOf chkEvenItalic_CheckedChanged
        AddHandler chkOddBold.CheckedChanged, AddressOf chkOddBold_CheckedChanged
        AddHandler chkOddItalic.CheckedChanged, AddressOf chkOddItalic_CheckedChanged
    End Sub

    Private Sub ApplySettings()
        cboHeaderFont.SelectedItem = Settings.HeaderSettings.FontName
        txtHeaderFontSize.Value = Settings.HeaderSettings.FontSize
        chkHeaderBold.Checked = Settings.HeaderSettings.FontBold
        chkHeaderItalic.Checked = Settings.HeaderSettings.FontItalic
        cmdHeaderForeColor.BackColor = Settings.HeaderSettings.ForeColor
        cmdHeaderForeColor.ForeColor = Common.ReversedColor(Settings.HeaderSettings.ForeColor)
        cmdHeaderBackColor.BackColor = Settings.HeaderSettings.BackColor
        cmdHeaderBackColor.ForeColor = Common.ReversedColor(Settings.HeaderSettings.BackColor)

        cboEvenFont.SelectedItem = Settings.EvenSettings.FontName
        txtEvenFontSize.Value = Settings.EvenSettings.FontSize
        chkEvenBold.Checked = Settings.EvenSettings.FontBold
        chkEvenItalic.Checked = Settings.EvenSettings.FontItalic
        cmdEvenForeColor.BackColor = Settings.EvenSettings.ForeColor
        cmdEvenForeColor.ForeColor = Common.ReversedColor(Settings.EvenSettings.ForeColor)
        cmdEvenBackColor.BackColor = Settings.EvenSettings.BackColor
        cmdEvenBackColor.ForeColor = Common.ReversedColor(Settings.EvenSettings.BackColor)

        cboOddFont.SelectedItem = Settings.OddSettings.FontName
        txtOddFontSize.Value = Settings.OddSettings.FontSize
        chkOddBold.Checked = Settings.OddSettings.FontBold
        chkOddItalic.Checked = Settings.OddSettings.FontItalic
        cmdOddForeColor.BackColor = Settings.OddSettings.ForeColor
        cmdOddForeColor.ForeColor = Common.ReversedColor(Settings.OddSettings.ForeColor)
        cmdOddBackColor.BackColor = Settings.OddSettings.BackColor
        cmdOddBackColor.ForeColor = Common.ReversedColor(Settings.OddSettings.BackColor)

    End Sub
    Private Sub InitCbo()
        InitCbo(cboHeaderFont)
        InitCbo(cboEvenFont)
        InitCbo(cboOddFont)
    End Sub

    Private Sub InitCbo(ByVal AComboBox As ComboBox)
        AComboBox.Items.Clear()
        AComboBox.DrawMode = DrawMode.OwnerDrawFixed
        AComboBox.ItemHeight = 20
        AddHandler AComboBox.DrawItem, AddressOf cboFont_DrawItem
        For i As Integer = 0 To Settings.FontList.Count - 1
            AComboBox.Items.Add(Settings.FontList(i).Name)
        Next
    End Sub

    Private Sub InitSample()
        Grid.Rows.Clear()
        For i As Integer = 0 To SAMPLE_ROW_COUNT - 1
            Grid.Rows.Add("aaaa", "1234", "ああああ", "いいいい")
        Next
    End Sub

    Private Sub cboFont_DrawItem(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.DrawItemEventArgs)
        e.DrawBackground()
        Dim cmb As ComboBox = CType(sender, ComboBox)
        '項目に表示する文字列
        Dim txt As String
        If e.Index > -1 Then
            txt = cmb.Items(e.Index).ToString()
        Else
            txt = cmb.Text
        End If
        '使用するフォント
        Dim f As New Font(txt, cmb.Font.Size)
        '使用するブラシ
        Dim b = New SolidBrush(e.ForeColor)
        '文字列を描画する
        Dim ym As Single = _
            (e.Bounds.Height - e.Graphics.MeasureString(txt, f).Height) / 2
        e.Graphics.DrawString(txt, f, b, e.Bounds.X, e.Bounds.Y + ym)
        f.Dispose()
        b.Dispose()
        'フォーカスを示す四角形を描画
        e.DrawFocusRectangle()
    End Sub

    Private Sub cboHeaderFont_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboHeaderFont.SelectedIndexChanged
        Settings.HeaderSettings.FontName = cboHeaderFont.Text
        Settings.Apply(Grid)
    End Sub

    Private Sub txtHeaderFontSize_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtHeaderFontSize.ValueChanged
        Settings.HeaderSettings.FontSize = txtHeaderFontSize.Value
        Settings.Apply(Grid)
    End Sub

    Private Sub cboEvenFont_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboEvenFont.SelectedIndexChanged
        Settings.EvenSettings.FontName = cboEvenFont.Text
        Settings.Apply(Grid)
    End Sub

    Private Sub txtEvenFontSize_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtEvenFontSize.ValueChanged
        Settings.EvenSettings.FontSize = txtEvenFontSize.Value
        Settings.Apply(Grid)
    End Sub

    Private Sub cboOddFont_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboOddFont.SelectedIndexChanged
        Settings.OddSettings.FontName = cboOddFont.Text
        Settings.Apply(Grid)
    End Sub

    Private Sub txtOddFontSize_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtOddFontSize.ValueChanged
        Settings.OddSettings.FontSize = txtOddFontSize.Value
        Settings.Apply(Grid)
    End Sub

    Private Sub chkHeaderBold_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkHeaderBold.CheckedChanged
        Settings.HeaderSettings.FontBold = chkHeaderBold.Checked
        Settings.Apply(Grid)
    End Sub

    Private Sub chkHeaderItalic_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkHeaderItalic.CheckedChanged
        Settings.HeaderSettings.FontItalic = chkHeaderItalic.Checked
        Settings.Apply(Grid)
    End Sub

    Private Sub chkEvenBold_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkEvenBold.CheckedChanged
        Settings.EvenSettings.FontBold = chkEvenBold.Checked
        Settings.Apply(Grid)
    End Sub

    Private Sub chkEvenItalic_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkEvenItalic.CheckedChanged
        Settings.EvenSettings.FontItalic = chkEvenItalic.Checked
        Settings.Apply(Grid)
    End Sub

    Private Sub chkOddBold_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkOddBold.CheckedChanged
        Settings.OddSettings.FontBold = chkOddBold.Checked
        Settings.Apply(Grid)
    End Sub

    Private Sub chkOddItalic_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkOddItalic.CheckedChanged
        Settings.OddSettings.FontItalic = chkOddItalic.Checked
        Settings.Apply(Grid)
    End Sub

    Private Sub cmdOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOK.Click
        Settings.SaveConfig()
        _ModalResult = Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
        Settings.LoadConfig()
        _ModalResult = Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub cmdHeaderForeColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdHeaderForeColor.Click
        ColorDlg.Color = cmdHeaderForeColor.BackColor
        If ColorDlg.ShowDialog = Windows.Forms.DialogResult.OK Then
            cmdHeaderForeColor.BackColor = ColorDlg.Color
            cmdHeaderForeColor.ForeColor = Common.ReversedColor(ColorDlg.Color)
            Settings.HeaderSettings.ForeColor = ColorDlg.Color
            Settings.Apply(Grid)
        End If
    End Sub

    Private Sub cmdEvenForeColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEvenForeColor.Click
        ColorDlg.Color = cmdEvenForeColor.BackColor
        If ColorDlg.ShowDialog = Windows.Forms.DialogResult.OK Then
            cmdEvenForeColor.BackColor = ColorDlg.Color
            Settings.EvenSettings.ForeColor = ColorDlg.Color
            cmdEvenForeColor.ForeColor = Common.ReversedColor(ColorDlg.Color)
            Settings.Apply(Grid)
        End If
    End Sub

    Private Sub cmdOddForeColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOddForeColor.Click
        ColorDlg.Color = cmdOddForeColor.BackColor
        If ColorDlg.ShowDialog = Windows.Forms.DialogResult.OK Then
            cmdOddForeColor.BackColor = ColorDlg.Color
            Settings.OddSettings.ForeColor = ColorDlg.Color
            cmdOddForeColor.ForeColor = Common.ReversedColor(ColorDlg.Color)
            Settings.Apply(Grid)
        End If
    End Sub

    Private Sub cmdHeaderBackColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdHeaderBackColor.Click
        ColorDlg.Color = cmdHeaderBackColor.BackColor
        If ColorDlg.ShowDialog = Windows.Forms.DialogResult.OK Then
            cmdHeaderBackColor.BackColor = ColorDlg.Color
            cmdHeaderBackColor.ForeColor = Common.ReversedColor(ColorDlg.Color)
            Settings.HeaderSettings.BackColor = ColorDlg.Color
            Settings.Apply(Grid)
        End If
    End Sub

    Private Sub cmdEvenBackColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEvenBackColor.Click
        ColorDlg.Color = cmdEvenBackColor.BackColor
        If ColorDlg.ShowDialog = Windows.Forms.DialogResult.OK Then
            cmdEvenBackColor.BackColor = ColorDlg.Color
            cmdEvenBackColor.ForeColor = Common.ReversedColor(ColorDlg.Color)
            Settings.EvenSettings.BackColor = ColorDlg.Color
            Settings.Apply(Grid)
        End If
    End Sub

    Private Sub cmdOddBackColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOddBackColor.Click
        ColorDlg.Color = cmdOddBackColor.BackColor
        If ColorDlg.ShowDialog = Windows.Forms.DialogResult.OK Then
            cmdOddBackColor.BackColor = ColorDlg.Color
            cmdOddBackColor.ForeColor = Common.ReversedColor(ColorDlg.Color)
            Settings.OddSettings.BackColor = ColorDlg.Color
            Settings.Apply(Grid)
        End If
    End Sub
End Class